package com.gmt.handler;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author John.zhang
 * @version Id: MyHandlerExceptionResolver, v 1.0 2020/11/12 14:33 ZhiYong Exp $
 */
@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        if(e instanceof  NullPointerException){
            modelAndView.setViewName("errorA");
            modelAndView.addObject("error","空指针异常");
        }else if(e instanceof  ArithmeticException){
            modelAndView.setViewName("errorB");
            modelAndView.addObject("error","算数异常");
        }
        return modelAndView;
    }
}
